package com.assistant.utils;

import com.assistant.SELECT.ChooseFileType;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class CommonComponent {
    //信息提示框
    public static void warningAlert(Component parent,String title, String content) {
      JOptionPane pane=new JOptionPane(content,JOptionPane.INFORMATION_MESSAGE);
      JDialog jDialog=pane.createDialog(title);
      jDialog.setResizable(true);
      jDialog.setVisible(true);
    }
    //进度条初始化
    public static JProgressBar createProcessBar(){
        JFrame jFrame=new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setLocation(600,420);
        jFrame.setSize(300,80);
        Container container=jFrame.getContentPane();
        JPanel jPanel=new JPanel();
        JProgressBar jpb=new JProgressBar(SwingConstants.HORIZONTAL,0,100);
        container.add(jPanel);
        jPanel.add(jpb);
        jpb.setValue(0);
        jpb.setStringPainted(true);
        jFrame.setVisible(true);
        Button ok=new Button("确定");
        ok.setEnabled(false);
        ok.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
               jFrame.dispose();
            }
        });
        jpb.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if(jpb.getValue()==jpb.getMaximum()){
                    ok.setEnabled(true);
                }else{
                    ok.setEnabled(false);
                }
            }
        });
        jPanel.add(ok);
        return jpb;
    }

    /**
     * @param path 在哪个目录下打开，如果为 null，默认为根目录下打开
     * @param type 选择需要打开的文件类型
     * @param extensions 选择需要过滤的文件扩展名
     * @return File  返回选中的文件
     */
    //文件选择器
    public static File fileChooser(JPanel jPanel,String path,ChooseFileType type,String... extensions){
        JFileChooser jFileChooser;
        if(path==null)
             jFileChooser=new JFileChooser();
        else
            jFileChooser=new JFileChooser(path);
        switch (type){
            case FILES:
                jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            case DIRECTORY:
                jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            case FILES_AND_DIRECTORIES:
                jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        }
        jFileChooser.setMultiSelectionEnabled(false);
        FileFilter fileFilter=new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.isDirectory()){
                    return true;
                }
                String filename=pathname.getName().toLowerCase();
                int i=0;
                while(i<extensions.length){
                    if(filename.endsWith(extensions[i])){
                        return true;
                    }
                    i++;
                }
                return false;
            }

            @Override
            public String getDescription() {
                return extensions.toString();
            }
        };
        jFileChooser.setFileFilter(fileFilter);
        int returnValue=jFileChooser.showOpenDialog(null);
        if(returnValue==JFileChooser.APPROVE_OPTION){
            File selectedFile=jFileChooser.getSelectedFile();
            return selectedFile;
        }else{
            CommonComponent.warningAlert(jPanel,"警告","您选择的文件不符合要求！");
        }
        return null;
    }
}
